package com.highcom.dao.impl;

import com.highcom.bean.AdminInfo;
import com.highcom.dao.AdminInfoDao;
import com.highcom.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-10-09
 */
public class AdminInfoDaoImpl implements AdminInfoDao {

    /**
     * 按照用户名查找用户
     * @param adminName 用户名
     * @return 用户对象
     */
    @Override
    public AdminInfo selectByAdminName(String adminName) {
        DBUtil.connect();
        ResultSet rs = null;
        AdminInfo adminInfo = null;
        try {
            rs = DBUtil.executeQuery("select * from admininfo where adminName=?", adminName);
            if (rs.next()) {
                adminInfo = new AdminInfo();
                adminInfo.setId(rs.getInt("id"));
                adminInfo.setAdminName(rs.getString("adminName"));
                adminInfo.setEditDate(rs.getString("editDate"));
                adminInfo.setNote(rs.getString("note"));
                adminInfo.setPassword(rs.getString("password"));
                adminInfo.setState(rs.getInt("state"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs);
        }
        return adminInfo;
    }

    @Override
    public Integer insert(AdminInfo adminInfo) {
        DBUtil.connect();
        DBUtil.open();
        try {
            return DBUtil.executeInsertOfGeneratedKey("insert into admininfo(note, password, adminName, state) values(?,?,?,?,?)",
                    adminInfo.getNote(), adminInfo.getPassword(), adminInfo.getAdminName(), adminInfo.getState());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }
}
